export const useMyUserLoginStoreStore = defineStore("myUserLoginStoreStore", {
  state: () => ({
    agree: undefined as boolean | undefined,
    usr: undefined as string | undefined,
    pwd: undefined as string | undefined,
    phone: undefined as string | undefined,
    code: undefined as string | undefined,
    // 发送验证码倒计时及最后点击的时间的记录存储
    lastTimeClick: 0,
    time: 30,
  }),
  actions: {
    /**
     * 初始化实例所在区域的用户数据
     *
     * @param userData 用户数据，默认为 undefined。如果传入 true，则设置 this.agree 为 true；如果传入 false，则设置 this.agree 为 false；如果未传入或传入 undefined，则 this.agree 保持原值。
     */
    initUserDataForInstanceInRegion(userData?: boolean | undefined) {
      this.agree = userData || false;
      this.code = "";
      this.usr = "";
      this.pwd = "";
      this.phone = "";
    },

    /**
     * 用户登录行为中检查用户是否同意协议
     *
     * 如果用户未同意协议，则抛出错误
     */
    userLoginActionForIsAgreement() {
      // 如果用户同意协议，则不做任何操作
      if (this.agree) { return; }
      // 如果用户未同意协议，则抛出错误
      const error = localesGetValueForKey("errorsMessages.userLoginError.AgreementIsFalse", "请先阅读并且同意用户协议和隐私协议") as string
      throw new Error(error);
    }
  },
  persist: {
    storage: piniaPluginPersistedstate.sessionStorage(),
  },
});
